#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#  Copyright (c) Huoty - All rights reserved
# 
#      Author: Huoty <sudohuoty@gmail.com>
#  CreateTime: 2016-10-25 14:35:55
# *************************************************************

from __future__ import print_function
import sys
import pip
from importlib import import_module


class AutoInstall(object):
    _loaded = set()

    @classmethod
    def find_module(cls, name, path, target=None):
        if path is None and name not in cls._loaded:
            cls._loaded.add(name)
            print("Installing", name)
            installed = pip.main(["install", name])
            if installed == 0:
                return import_module(name)
            else:    
                return None

sys.meta_path.append(AutoInstall)

